import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex)

const storageData = (data) => {
	return uni.getStorageSync(data)
}

const store = new Vuex.Store({
	state: {
		token: '',
		userInfo: {}
	},
	mutations: {
		$Store(state, payload) {
			// 判断是否多层级调用，state中为对象存在的情况，诸如user.info.score = 1
			let nameArr = payload.name.split('.');
			let saveKey = '';
			let len = nameArr.length;
			if(len >= 2) {
				let obj = state[nameArr[0]];
				for(let i = 1; i < len - 1; i ++) {
					obj = obj[nameArr[i]];
				}
				obj[nameArr[len - 1]] = payload.value;
				saveKey = nameArr[0];
			} else {
				// 单层级变量，在state就是一个普通变量的情况
				state[payload.name] = payload.value;
				saveKey = payload.name;
			}
			// 保存变量到本地，见顶部函数定义
			saveLifeData(saveKey, state[saveKey])
		},
		SET_USERINFO(state, data) {
			if (data) {
				state.userInfo = data
			} else {
				state.userInfo = storageData('userInfo')
			}
		},
		SET_TOKEN(state, data) {
			if (data) {
				state.token = data
			} else {
				state.token = storageData('token')
			}
		}
	},
	actions: {},
	getters: {}
})

export default store
